import { Reducer, Effect, Subscription } from 'umi';
import {
  getnoticeList, //学生获取通知列表
  getnoticeDetail, //学生获取通知详情
  getfileDownload, //下载附件
} from '@/services/noticeforstu';
import {getfileUpload} from '@/services/fileService'
//对state声明，state用于仓库存储数据，也就是说state就是仓库
export type StateType = {
  list?: any[];
  studentScoreList?: any[];
};
//对model声明
export type noticeforstuModelType = {
  namespace: string;
  state: StateType;
  effects: {
    getNoticeListEffects: Effect; //学生获取通知列表
    getNoticeDetailEffects: Effect; //学生获取通知详情
    getFileDownloadEffects: Effect; //下载附件
    getfileUploadEffects: Effect; //填写回执带附件
  };
  reducers: {};
  subscriptions: {
    // setup: Subscription;
  };
};

const noticeforstuModel: noticeforstuModelType = {
  namespace: 'Model_noticeforstuModel',
  state: {
    list: [],
    studentScoreList: [],
  },
  effects: {
    *getNoticeListEffects({ payload }: any, { call, put }: any): Generator {
      //学生获取通知列表
      // let token = localStorage.getItem('token');
      const result: any = yield call(getnoticeList, '', payload.code);
      const transformeData = result.result.map((item: any) => ({
        ...item,
        status: parseInt(item.status, 10),
        key: item.id,
      }));

      payload.callback(transformeData);
    },
    *getNoticeDetailEffects({ payload }: any, { call, put }: any): Generator {
      //学生获取通知详情
      //let token = localStorage.getItem('token');
      const result: any = yield call(getnoticeDetail, '', payload.code);
      // console.log(result.result);
      payload.callback(result.result);
    },
    *getFileDownloadEffects({ payload }: any, { call, put }: any): Generator {
      //下载附件
      //let token = localStorage.getItem('token');
      const result: any = yield call(getfileDownload, '', payload.code);
      // console.log(result.result);
      payload.callback(result);
    },
    *getfileUploadEffects({ payload }: any, { call, put }: any): Generator {
      //填写回执带附件
      //let token = localStorage.getItem('token');
      const result: any = yield call(getfileUpload, '', payload.data);
      // console.log(result.result);
      payload.callback(result);
    },

  },
  //命名规范（个人）：函数功能+Reducers
  reducers: {},
  subscriptions: {},
};

export default noticeforstuModel;
